package com.cxg.fairy.ec.detail;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.ListViewCompat;
import android.view.View;
import android.widget.ListView;

import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.ui.loader.LatteLoader;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;
import com.cxg.fairy.util.log.LatteLogger;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;

import java.util.List;

import butterknife.BindView;

/**
 * Created by gabrielsung on 2018/4/6 0006.
 */

public class CommentListDelegate extends LatteDelegate implements ISuccess{

    List<MultipleItemEntity> data = null;

    private static final String COMMENT_TYPE = "COMMENT_TYPE";

    private static final String SKU_ID = "SKU_ID";

    @BindView(R2.id.rv_comment_list)
    ListViewCompat commentListView = null;

    @Override
    public Object setLayout() {
        return R.layout.delegate_goods_detail_comment_listview;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        initData(getArguments().getInt(COMMENT_TYPE),getArguments().getInt(SKU_ID));
    }

    public static CommentListDelegate create(int commentType,int skuId) {
        final Bundle args = new Bundle();
        args.putInt(COMMENT_TYPE, commentType);
        args.putInt(SKU_ID, skuId);
        final CommentListDelegate delegate = new CommentListDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    private void initData(int commentType,int skuId) {
        RestClient.builder()
                .loader(getContext())
                .url("getGoodsEvalByGoodsId_bySortType")
                .params("uuid", AccountManager.getSignUUID())
                .params("skuId",skuId)
                .params("sortType",commentType)
                .success(this)
                .build()
                .post();
    }

    @Override
    public void onSuccess(String response) {
        data = new CommentListDataConverter().setJsonData(response).convert();
        final CommentListAdapter commentListAdapter = new CommentListAdapter(getContext(), R.layout.item_goods_detail_comment, data);
        commentListView.setDividerHeight(0);
        commentListView.setAdapter(commentListAdapter);
    }
}
